home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / Delphi 3.0 / DATA.Z / extdlgs.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-01-29  |  7.4 KB  |  287 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Delphi Visual Component Library                 }
  5. {                                                       }
  6. {       Copyright (c) 1995,97 Borland International     }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit ExtDlgs;
  11.  
  12. {$R-}
  13.  
  14. interface
  15.  
  16. uses Messages, Windows, SysUtils, Classes, Controls, StdCtrls, Graphics,
  17.   ExtCtrls, Buttons, Dialogs;
  18.  
  19. type
  20.  
  21. { TOpenPictureDialog }
  22.  
  23.   TOpenPictureDialog = class(TOpenDialog)
  24.   private
  25.     FPicture: TPicture;
  26.     FPicturePanel: TPanel;
  27.     FPictureLabel: TLabel;
  28.     FPreviewButton: TSpeedButton;
  29.     FPaintPanel: TPanel;
  30.     FPaintBox: TPaintBox;
  31.     procedure PaintBoxPaint(Sender: TObject);
  32.     procedure PreviewClick(Sender: TObject);
  33.     procedure PreviewKeyPress(Sender: TObject; var Key: Char);
  34.   protected
  35.     procedure DoClose; override;
  36.     procedure DoSelectionChange; override;
  37.     procedure DoShow; override;
  38.   public
  39.     constructor Create(AOwner: TComponent); override;
  40.     destructor Destroy; override;
  41.     function Execute: Boolean; override;
  42.   end;
  43.  
  44. { TSavePictureDialog }
  45.  
  46.   TSavePictureDialog = class(TOpenPictureDialog)
  47.     function Execute: Boolean; override;
  48.   end;
  49.  
  50. implementation
  51.  
  52. uses Consts, Forms, CommDlg, Dlgs;
  53.  
  54. { TOpenPictureDialog }
  55.  
  56. {$R EXTDLGS.RES}
  57.  
  58. constructor TOpenPictureDialog.Create(AOwner: TComponent);
  59. begin
  60.   inherited Create(AOwner);
  61.   Filter := GraphicFilter(TGraphic);
  62.   FPicture := TPicture.Create;
  63.   FPicturePanel := TPanel.Create(Self);
  64.   with FPicturePanel do
  65.   begin
  66.     Name := 'PicturePanel';
  67.     Caption := '';
  68.     SetBounds(204, 5, 169, 200);
  69.     BevelOuter := bvNone;
  70.     BorderWidth := 6;
  71.     TabOrder := 1;
  72.     FPictureLabel := TLabel.Create(Self);
  73.     with FPictureLabel do
  74.     begin
  75.       Name := 'PictureLabel';
  76.       Caption := '';
  77.       SetBounds(6, 6, 157, 23);
  78.       Align := alTop;
  79.       AutoSize := False;
  80.       Parent := FPicturePanel;
  81.     end;
  82.     FPreviewButton := TSpeedButton.Create(Self);
  83.     with FPreviewButton do
  84.     begin
  85.       Name := 'PreviewButton';
  86.       SetBounds(77, 1, 23, 22);
  87.       Enabled := False;
  88.       Glyph.LoadFromResourceName(HInstance, 'PREVIEWGLYPH');
  89.       Hint := SPreviewLabel;
  90.       ParentShowHint := False;
  91.       ShowHint := True;
  92.       OnClick := PreviewClick;
  93.       Parent := FPicturePanel;
  94.     end;
  95.     FPaintPanel := TPanel.Create(Self);
  96.     with FPaintPanel do
  97.     begin
  98.       Name := 'PaintPanel';
  99.       Caption := '';
  100.       SetBounds(6, 29, 157, 145);
  101.       Align := alClient;
  102.       BevelInner := bvRaised;
  103.       BevelOuter := bvLowered;
  104.       TabOrder := 0;
  105.       FPaintBox := TPaintBox.Create(Self);
  106.       Parent := FPicturePanel;
  107.       with FPaintBox do
  108.       begin
  109.         Name := 'PaintBox';
  110.         SetBounds(0, 0, 153, 141);
  111.         Align := alClient;
  112.         OnDblClick := PreviewClick;
  113.         OnPaint := PaintBoxPaint;
  114.         Parent := FPaintPanel;
  115.       end;
  116.     end;
  117.   end;
  118. end;
  119.  
  120. destructor TOpenPictureDialog.Destroy;
  121. begin
  122.   FPaintBox.Free;
  123.   FPaintPanel.Free;
  124.   FPreviewButton.Free;
  125.   FPictureLabel.Free;
  126.   FPicturePanel.Free;
  127.   FPicture.Free;
  128.   inherited Destroy;
  129. end;
  130.  
  131. procedure TOpenPictureDialog.DoSelectionChange;
  132. var
  133.   FullName: string;
  134.   ValidPicture: Boolean;
  135.  
  136.   function ValidFile(const FileName: string): Boolean;
  137.   begin
  138.     Result := GetFileAttributes(PChar(FileName)) <> $FFFFFFFF;
  139.   end;
  140.  
  141. begin
  142.   FullName := FileName;
  143.   ValidPicture := FileExists(FullName) and ValidFile(FullName);
  144.   if ValidPicture then
  145.   try
  146.     FPicture.LoadFromFile(FullName);
  147.     FPictureLabel.Caption := Format(SPictureDesc, [FPicture.Width,
  148.       FPicture.Height]);
  149.     FPreviewButton.Enabled := True;
  150.   except
  151.     ValidPicture := False;
  152.   end;
  153.   if not ValidPicture then
  154.   begin
  155.     FPictureLabel.Caption := SPictureLabel;
  156.     FPreviewButton.Enabled := False;
  157.     FPicture.Assign(nil);
  158.   end;
  159.   FPaintBox.Invalidate;
  160.   inherited DoSelectionChange;
  161. end;
  162.  
  163. procedure TOpenPictureDialog.DoClose;
  164. begin
  165.   inherited DoClose;
  166.   { Hide any hint windows left behind }
  167.   Application.HideHint;
  168. end;
  169.  
  170. procedure TOpenPictureDialog.DoShow;
  171. var
  172.   PreviewRect, StaticRect: TRect;
  173. begin
  174.   { Set preview area to entire dialog }
  175.   GetClientRect(Handle, PreviewRect);
  176.   StaticRect := GetStaticRect;
  177.   { Move preview area to right of static area }
  178.   PreviewRect.Left := StaticRect.Left + (StaticRect.Right - StaticRect.Left);
  179.   Inc(PreviewRect.Top, 4);
  180.   FPicturePanel.BoundsRect := PreviewRect;
  181.   FPreviewButton.Left := FPaintPanel.BoundsRect.Right - FPreviewButton.Width - 2;
  182.   FPicture.Assign(nil);
  183.   FPicturePanel.ParentWindow := Handle;
  184.   inherited DoShow;
  185. end;
  186.  
  187. function TOpenPictureDialog.Execute;
  188. begin
  189.   if NewStyleControls and not (ofOldStyleDialog in Options) then
  190.     Template := 'DLGTEMPLATE' else
  191.     Template := nil;
  192.   Result := inherited Execute;
  193. end;
  194.  
  195. procedure TOpenPictureDialog.PaintBoxPaint(Sender: TObject);
  196. var
  197.   DrawRect: TRect;
  198.   SNone: string;
  199. begin
  200.   with TPaintBox(Sender) do
  201.   begin
  202.     Canvas.Brush.Color := Color;
  203.     DrawRect := ClientRect;
  204.     if FPicture.Width > 0 then
  205.     begin
  206.       with DrawRect do
  207.         if (FPicture.Width > Right - Left) or (FPicture.Height > Bottom - Top) then
  208.         begin
  209.           if FPicture.Width > FPicture.Height then
  210.             Bottom := Top + MulDiv(FPicture.Height, Right - Left, FPicture.Width)
  211.           else
  212.             Right := Left + MulDiv(FPicture.Width, Bottom - Top, FPicture.Height);
  213.           Canvas.StretchDraw(DrawRect, FPicture.Graphic);
  214.         end
  215.         else
  216.           with DrawRect do
  217.             Canvas.Draw(Left + (Right - Left - FPicture.Width) div 2, Top + (Bottom - Top -
  218.               FPicture.Height) div 2, FPicture.Graphic);
  219.     end
  220.     else
  221.       with DrawRect, Canvas do
  222.       begin
  223.         SNone := srNone;
  224.         TextOut(Left + (Right - Left - TextWidth(SNone)) div 2, Top + (Bottom -
  225.           Top - TextHeight(SNone)) div 2, SNone);
  226.       end;
  227.   end;
  228. end;
  229.  
  230. procedure TOpenPictureDialog.PreviewClick(Sender: TObject);
  231. var
  232.   PreviewForm: TForm;
  233.   Panel: TPanel;
  234. begin
  235.   PreviewForm := TForm.Create(Self);
  236.   with PreviewForm do
  237.   try
  238.     Name := 'PreviewForm';
  239.     Caption := SPreviewLabel;
  240.     BorderStyle := bsSizeToolWin;
  241.     KeyPreview := True;
  242.     Position := poScreenCenter;
  243.     OnKeyPress := PreviewKeyPress;
  244.     Panel := TPanel.Create(PreviewForm);
  245.     with Panel do
  246.     begin
  247.       Name := 'Panel';
  248.       Caption := '';
  249.       Align := alClient;
  250.       BevelOuter := bvNone;
  251.       BorderStyle := bsSingle;
  252.       Parent := PreviewForm;
  253.       with TImage.Create(PreviewForm) do
  254.       begin
  255.         Name := 'Image';
  256.         Caption := '';
  257.         Align := alClient;
  258.         Stretch := True;
  259.         Picture.Assign(FPicture);
  260.         Parent := Panel;
  261.       end;
  262.     end;
  263.     if FPicture.Width > 0 then
  264.     begin
  265.       ClientWidth := FPicture.Width + (ClientWidth - Panel.ClientWidth);
  266.       ClientHeight := FPicture.Height + (ClientHeight - Panel.ClientHeight);
  267.     end;
  268.     ShowModal;
  269.   finally
  270.     Free;
  271.   end;
  272. end;
  273.  
  274. procedure TOpenPictureDialog.PreviewKeyPress(Sender: TObject; var Key: Char);
  275. begin
  276.   if Key = #27 then TForm(Sender).Close;
  277. end;
  278.  
  279. { TSavePictureDialog }
  280.  
  281. function TSavePictureDialog.Execute: Boolean;
  282. begin
  283.   Result := DoExecute(@GetSaveFileName);
  284. end;
  285.  
  286. end.
  287.